package com.dyrj.pig.controller;

import com.dyrj.pig.bean.TYskBindcard;
import com.dyrj.pig.service.TYskBindcardService;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.dyrj.ysk.configuration.SdkProperties;
import com.dyrj.ysk.exception.BizException;
import com.dyrj.ysk.service.SignService;
import com.dyrj.ysk.util.CusAccessObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName: TYskBindcardController
 * @Description: 5.4	无跳转快捷绑卡后台通知
 * 付款人在银联页面绑卡成功后，云收款系统异步接收银联的后端通知结果，
 * 处理完成后，异步通知商户绑卡结果，商户需要接收处理，并将绑卡相关信息以POST方法json格式发送至后台通知URL。
 * 通知会发送三次，商户响应“notify_success”表示接收成功。在任意次通知时，商户正确响应，通知立刻结束，不再重复发送。
 * @author cyberdolphin
 */
@Controller
@RequestMapping("/ysk/bindcard")
@Slf4j
public class TYskBindcardController {

	@Resource
	private TYskBindcardService service;
	@Resource
	private SdkProperties sdkProperties;
	@Resource
	private SignService signService;

//	/**
//	 * @Title: notify
//	 * @Description: (通知)
//	 * @param map
//	 * @return ResultMsg    返回类型
//	 */
//	@PostMapping("/notify")
//	@ResponseBody
////	public String notify(@RequestBody Map map) {
//	public String notify(@RequestParam Map map) {
//		String rtnv = "notify_success";
//		String str_json = JSONObject.toJSONString(map);
//		log.debug("TYskBindcard str_json"+str_json);
//		return rtnv;
//	}

	/**
	 * CLOUDPAY6007
	 * 商户绑卡后台通知
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws BizException
	 */
	@RequestMapping(value = "/notify",method = RequestMethod.POST)
	@ResponseBody
	public void bindCardNotifyUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, BizException {
		log.debug("bindCardNotifyUrl_beg");
		Map<String,String> params= CusAccessObjectUtil.getAllRequestParam(request);
		log.info(params.toString());
		service.bindCardNotify(params);
		try{
			//验签
			String sign=params.remove("Signature");
			boolean verify=signService.verify(params,sign,sdkProperties.getPublicKey());
			log.info("验签结果："+verify);
		}catch (Exception e){
		    e.printStackTrace();
		}
		log.debug("bindCardNotifyUrl_end");
		response.addHeader("Content-Type", "text/html;charset=utf-8");
		response.getWriter().write("notify_success");

	}

	@RequestMapping(value = "/jumpUrl",method = RequestMethod.POST)
	public String jumpUrl(HttpServletRequest request, Model model) throws IOException, BizException {
		log.debug("bindCardjumpUrl_beg");
		try{
			Map<String,String> params=CusAccessObjectUtil.getAllRequestParam(request);
			log.info(params.toString());
			model.addAttribute("params", params);
			//验签
			String sign=params.remove("Signature");
			boolean verify=signService.verify(params,sign,sdkProperties.getPublicKey());
			log.info("验签结果："+verify);
		}catch (Exception e){
			e.printStackTrace();
		}
		log.debug("bindCardjumpUrl_end");
		return "ysksdk/jumpUrlToMobileBank";
	}

}
